package com.quanyan.ic.datacenter.hik8710.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.quanyan.ic.common.utils.DataEncryptionUtils;
import com.quanyan.ic.common.utils.HttpClientUtils;
import com.quanyan.ic.datacenter.hik8710.model.ImgTriggerInfo;
import com.quanyan.ic.datacenter.hik8710.model.Response;
import com.quanyan.ic.datacenter.hik8710.model.Trigger;
import com.quanyan.ic.datacenter.hik8710.service.Hik8710Service;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhuo.shi on 2017/8/28.
 */
@Service
public class Hik8710ServiceImpl implements Hik8710Service {

    @Value("${hik8710.server.appkey}")
    private String appkey;
    @Value("${hik8710.server.secret}")
    private String secret;
    @Value("${hik8710.server.api.baseUrl}")
    private String apiBaseUrl;
    @Value("${hik8710.server.img.baseUrl}")
    private String imgBaseUrl;

    @Override
    public List<String> getEventImgUrl(List<String> eventIds) {
        List<String> resultList = new ArrayList<>();
        if (CollectionUtils.isEmpty(eventIds)){
            return resultList;
        }
        long timestamp = System.currentTimeMillis();
        for (String eventId : eventIds) {
            String tokenStr = "/webapi/service/eps/getPlatTriggerLogseventLogUuid=" + eventId + "&appkey=" + appkey + "&time=" + timestamp + secret;
            String url= apiBaseUrl + "/webapi/service/eps/getPlatTriggerLogs?eventLogUuid="+eventId+"&appkey="+ appkey + "&time=" + timestamp + "&token=" + DataEncryptionUtils.MD5Encode(tokenStr);
            try {
                String result = HttpClientUtils.get(url);
                Response<List<Trigger>> resp = JSON.parseObject(result, new TypeReference<Response<List<Trigger>>>(){});
                if (resp == null || resp.getData() == null) {
                    continue;
                }
                List<Trigger> triggerList = resp.getData();
                for (Trigger trigger : triggerList){
                    if (trigger.getTriggerType() != 4){
                        continue;
                    }
                    String triggerInfoStr = trigger.getTriggerInfo();
                    if (StringUtils.isEmpty(triggerInfoStr)){
                        continue;
                    }
                    List<ImgTriggerInfo> imgTriggerInfoList = JSON.parseArray(triggerInfoStr, ImgTriggerInfo.class);
                    if (CollectionUtils.isEmpty(imgTriggerInfoList)) {
                        continue;
                    }
                    for (ImgTriggerInfo imgTriggerInfo : imgTriggerInfoList){
                        if (!StringUtils.isEmpty(imgTriggerInfo.getPicUrl())) {
                            resultList.add(imgTriggerInfo.getPicUrl());
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return resultList;
    }
}
